package com.hotel.customersystem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hotel.customersystem.mapper.PianhaoMapper;
import com.hotel.customersystem.pojo.Pianhao;
import com.hotel.customersystem.service.PianhaoService;
import com.hotel.hotelsystem.mapper.HotelListMapper;
import com.hotel.hotelsystem.pojo.HotelList;
import com.hotel.hotelsystem.service.HotelIndexRankingListService;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericItemBasedRecommender;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.ItemSimilarity;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 * (Pianhao)表服务实现类
 * @author makejava
 * @since 2022-11-04 09:25:31
 */
@Service("pianhaoService")
public class PianhaoServiceImpl extends ServiceImpl<PianhaoMapper, Pianhao> implements PianhaoService {
    @Autowired
    private DataModel dataModel;

    @Autowired
    private HotelListMapper hotelListMapper;


    @Override
    public List<HotelList> getRecommentProductByUser(Integer userId, Integer howMany) {
        List<HotelList> productList = null;
        try {
            /*计算相似度，相似度的计算方式很多，采用基于皮尔逊相关性的相似度*/
            UserSimilarity similarity = new PearsonCorrelationSimilarity(dataModel);
            /*
                计算最近邻居，邻居有两种算法：基于固定数量的邻居和基于相似度的邻居
                这里采用基于固定数量的邻居
            */
            UserNeighborhood userNeighborhood = new NearestNUserNeighborhood(100,similarity,dataModel);
            /*构建推荐器，基于用户的协同过滤推荐*/
            Recommender recommender = new GenericUserBasedRecommender(dataModel,userNeighborhood,similarity);
            long startTime = System.currentTimeMillis();
            /*推荐商品*/
            List<RecommendedItem> recommendedItemList =  recommender.recommend(userId,howMany);
            List<Integer> hotelIds =  new ArrayList<>();
            for(RecommendedItem recommendedItem : recommendedItemList){
                System.out.println("recommendedItem:" + recommendedItem);
                hotelIds.add((int) recommendedItem.getItemID());
            }
            System.out.println("推荐出来的 商品的 id集合:" + hotelIds);
            /*根据商品id 查询商品*/
            if(hotelIds != null && hotelIds.size() >0){
                productList = hotelListMapper.selectAllByIds(hotelIds);
            }else {
                productList = new ArrayList<>();
            }
            System.out.println("推荐数量是：" + productList.size() + ",耗时：" + (System.currentTimeMillis()-startTime));

        } catch (TasteException e) {
            e.printStackTrace();
            return productList;
        }
        return productList;
    }

    @Override
    public List<HotelList> getRecommentProductByProduct(Integer userId, Integer hotelId, Integer howMany) {
        List<HotelList> productList = null;

        try {
            /*计算相似度，相似度的计算方式很多，采用基于皮尔逊相关性的相似度*/
            ItemSimilarity itemSimilarity = new PearsonCorrelationSimilarity(dataModel);
            /*构建推荐器，基于物品的协同过滤推荐*/
            GenericItemBasedRecommender recommender = new GenericItemBasedRecommender(dataModel,itemSimilarity);
            long startTime = System.currentTimeMillis();
            /*推荐商品*/
            List<RecommendedItem> recommendedItemList =  recommender.recommendedBecause(userId,hotelId,howMany);
            List<Integer> proIds =  new ArrayList<>();
            for(RecommendedItem recommendedItem : recommendedItemList){
                System.out.println("recommendedItem:" + recommendedItem);
                proIds.add((int) recommendedItem.getItemID());
            }
            System.out.println("推荐出来的 商品的 id集合:" + proIds);
            /*根据商品id 查询商品*/
            if(proIds != null && proIds.size() >0){
                productList = hotelListMapper.selectAllByIds(proIds);
            }else {
                productList = new ArrayList<>();
            }
            System.out.println("推荐数量是：" + productList.size() + ",耗时：" + (System.currentTimeMillis()-startTime));
        } catch (TasteException e) {
            e.printStackTrace();
            return productList;
        }
        return productList;
    }
}

